<?php

class Danhmuc_Model_Category extends Zend_Db_Table_Abstract {

	public $_name = "tour_category";
	public function __construct($config = array()) {
		$this->_db = Zend_Db_Table::getDefaultAdapter();
		parent::__construct($config);
	}
	public function count($parameter) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = "(1=1)";
		if ($parameter['name'] != "") {
			$name = $parameter['name'];
			$where .= " and U.name LIKE '%" . $name . "%'";
		}
		if ($parameter['phamvi'] != "" && $parameter['phamvi'] != "2") {
			$phamvi = $parameter['phamvi'];
			$where .= " and U.status = '$phamvi'";
		}
		$sql = "select count(*) as COUNT from " . $this->_name
				. " U WHERE $where";
		$r = $db->query($sql)->fetch();
		return $r['COUNT'];
	}
	public function SelectAll($parameter, $limit, $offset) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = "(1=1)";
		$param = array();
		if ($parameter['name'] != "") {
			$name = $parameter['name'];
			$where .= " and U.name LIKE '%" . $name . "%'";
		}
		if ($parameter['website'] != "") {
			$website = $parameter['website'];
			$website .= " and U.website LIKE '%" . $website . "%'";
		}
		if ($parameter['phamvi'] != "" && $parameter['phamvi'] != "2") {
			$phamvi = $parameter['phamvi'];
			$where .= " and U.status = '$phamvi'";
		}
		$sql = "select * from " . $this->_name
				. " U WHERE $where limit $offset,$limit
	                ";
		$r = $db->query($sql)->fetchAll();
		return $r;
	}
	public function AddNewCatagory($params) {
		$id = $params['id'];
		$db = Zend_Db_Table::getDefaultAdapter();
		$data = array(
		            'name' => $params['name'],
		            'status'=> is_null($params['kichhoat']) ? 0 : 1
		);
		try {
			if($id ==0){
				$db->insert($this->_name, $data);
			}
			else{
				$db->update($this->_name, $data,"id = ".$id);
			}
		}catch (Zend_Exception $e) {
			echo ($e->getMessage());
			exit;
		}
	}
	public function CheckExitsName($name) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$sql = "select * from $this->_name where name='" . $name . "'";
		$stmt = $db->query($sql);
		$r = $stmt->fetch();
		if ($r)
			return 1;
		else
			return 0;
	}
	public function FindById($id) {
		$result = $this->getDefaultAdapter()
				->query("SELECT*FROM $this->_name WHERE id = ?", $id);
		return $result->fetch();
	}
}

